#!/bin/zsh

# include common
source $(realpath $(dirname "$0"))/common

# checkout shell type
if [ $SHELL_TYPE != 'zsh' ]; then
    log_error 'please use Zsh to execute this script!'
    return 1
fi

# deploy target path
tpath="$HOME/.genzsh"
mkdir -p ${tpath}

# script path
spath=$(dirname "$(realpath "$0")")
if [ "$spath" = ${tpath} ]; then
    log_error 'illegal position'
    return 1
fi

# copy files
find $spath \
        -maxdepth 1 \
        -not -path "${spath}" \
        -not -path "${spath}/.git/*" -not -path "${spath}/.git" \
        -not -path "${spath}/.vscode/*" -not -path "${spath}/.vscode" \
        -not -path "${spath}/plugins/*" -not -path "${spath}/plugins" \
        -not -name "README.md" -not -name "deploy.sh" -not -name "LICENSE" -not -name ".gitignore" \
        -exec echo cp -r {} ${tpath}/ \; -exec cp -r {} ${tpath}/ \;
if [ $? != 0 ]; then
    log_error 'copy file to $HOME failed'
    return 1
fi

# copy plugin's configuration files
mkdir -p "${tpath}/plugins"
find $spath/plugins \
        -maxdepth 1 \
        -name '*.config.sh' \
        -exec echo cp {} ${tpath}/plugins \; -exec cp {} ${tpath}/plugins \;
if [ $? != 0 ]; then
    log_error 'copy plugins file to $HOME failed'
    return 1
fi

# extract plugins
for script in $spath/plugins/*.deploy.sh; do
    . "$script"
done
